package com.caocaog.market.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.jeecg.common.exception.JeecgBootException;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Slf4j
public class DistanceUtil {

    public static Double getStreetDistance(double gpsFromLat, double gpsFromLng, double gpsToLat, double gpsToLng) {
        System.out.println("gpsFromLat = " + gpsFromLat + ", gpsFromLng = " + gpsFromLng + ", gpsToLat = " + gpsToLat + ", gpsToLng = " + gpsToLng);

        try {
            String url = "https://restapi.amap.com/v5/direction/bicycling?key=" + "d6b848bf716e1ca084cc568be85700f1";
            String rel = HttpUtil.get(url + "&origin=" + gpsFromLng + "," + gpsFromLat + "&destination=" + gpsToLng + "," + gpsToLat);
            JSONObject jsonObject = JSONUtil.parseObj(rel);
            // log.info("getDistanceMeter 距离请求"+jsonObject.get("status"));
            BigDecimal distance = new BigDecimal("0.01").setScale(2);
            if (jsonObject.get("status").equals("1")) {
                JSONObject route = JSONUtil.parseObj(jsonObject.get("route"));
                JSONArray paths = JSONUtil.parseArray(route.get("paths"));
                JSONObject pathsStr = JSONUtil.parseObj(paths.get(0));
                String distanceStr = (String) pathsStr.get("distance");
                distance = new BigDecimal(distanceStr).divide(new BigDecimal("1000")).setScale(2,BigDecimal.ROUND_UP);
               return distance.doubleValue();
            } else {
                String info = (String)jsonObject.get("info");
                String infocode = (String)jsonObject.get("infocode");
                throw new JeecgBootException(infocode+info);
            }
        } catch (Exception e) {
            log.error("DistanceUtil 距离计算错误:gpsFromLat:" + gpsFromLat + ",gpsFromLng:" + gpsFromLng +
                    ",gpsToLat:" + gpsToLat + ",gpsToLng:" + gpsToLng+" ======>"+e.getMessage());
            throw new JeecgBootException("距离计算失败~");
        }


    }

    public static double getDistanceMeter(double gpsFromLat,double gpsFromLng,double gpsToLat,double gpsToLng) {
        GlobalCoordinates source = new GlobalCoordinates(gpsFromLat,gpsFromLng);
        GlobalCoordinates target = new GlobalCoordinates(gpsToLat,gpsToLng);
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve( Ellipsoid.Sphere,source, target);

        BigDecimal distance = new BigDecimal(geoCurve.getEllipsoidalDistance());
//        System.out.println("Sphere坐标系计算结果："+distance + "米");
        BigDecimal b = distance.divide(new BigDecimal(1000)).setScale(2, RoundingMode.HALF_UP);
//        System.out.println("Sphere坐标系计算结果："+b  + "千米");
        if (b.compareTo(new BigDecimal("0.01"))<1){
            return 0.01;
        }
        return b.doubleValue();


    }
//    public static void main(String[] args) {
//
//        double meter1 = getDistanceMeter(34.741912, 113.777797, 34.714691, 113.640809);
//
//        System.out.println("Sphere坐标系计算结果：" + meter1 + "千米");
//    }
}
